<script setup>
import { onMounted, ref } from 'vue'
import { updateUser } from "@/api/login";
import { ElMessage } from "element-plus";

// 初始化用户账号和密码的状态
const username = ref('') // 假设这是用户的初始账号
const password = ref('') // 假设这是用户的初始密码

onMounted(() => {
  username.value = localStorage.getItem('username') || ''
  password.value = localStorage.getItem('password') || ''
})

// 函数用于保存更新后的账号和密码
const updateUserInfo = async () => {
  const res = await updateUser({ username: username.value, password: password.value })
  if (res.code == 200) {
    ElMessage.success('更新成功！')
    localStorage.setItem('username', username.value)
    localStorage.setItem('password', password.value)
  } else {
    ElMessage.error('更新失败！')
  }
}
</script>

<template>
  <div class="container">
    <div class="user-info-form">
      <h2>用户账号和密码管理</h2>
      <div class="form-group">
        <label for="username">账号</label>
        <input
            type="text"
            id="username"
            v-model="username"
            placeholder="请输入新账号"
        />
      </div>

      <div class="form-group">
        <label for="password">密码</label>
        <input
            type="password"
            id="password"
            v-model="password"
            placeholder="请输入新密码"
        />
      </div>

      <button @click="updateUserInfo">更新账号和密码</button>
    </div>
  </div>
</template>

<style scoped>
/* 外层容器居中显示 */
.container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #74ebd5 0%, #ACB6E5 100%);
  padding: 20px;
}

/* 表单卡片 */
.user-info-form {
  width: 100%;
  max-width: 400px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  padding: 30px;
  transition: transform 0.3s ease;
}

.user-info-form:hover {
  transform: translateY(-5px);
}

/* 标题样式 */
h2 {
  text-align: center;
  margin-bottom: 25px;
  color: #333;
  font-size: 1.5rem;
}

/* 表单组 */
.form-group {
  margin-bottom: 20px;
}

/* 标签样式 */
label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #555;
}

/* 输入框样式 */
input {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.3s ease;
}

input:focus {
  border-color: #007bff;
}

/* 按钮样式 */
button {
  width: 100%;
  padding: 12px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
}

button:hover {
  background-color: #0056b3;
  transform: scale(1.02);
}
</style>
